package com.wanxi.spring.cloud.words.service.impl;
import com.wanxi.spring.cloud.commons.api.CommonResult;
import com.wanxi.spring.cloud.words.mapper.WordsMapper;
import com.wanxi.spring.cloud.words.model.Words;
import com.wanxi.spring.cloud.words.service.WordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@Service
public class WordsServiceImpl implements WordsService {

    @Autowired(required = false)
    WordsMapper wordsMapper;

    @Override
    public CommonResult getWords(Map map) {
        int limit = Integer.parseInt((String)map.get("limit"));
        int page = (Integer.parseInt((String)map.get("page"))-1)*limit;
        map.put("page",page);
        map.put("limit",limit);
        List<Words> words = wordsMapper.getWords(map);
        long count = wordsMapper.getWordsCount();
        return CommonResult.success(words,count);
    }

    @Override
    public CommonResult getSelfWords(Map map) {
        int limit = Integer.parseInt((String)map.get("limit"));
        int page = (Integer.parseInt((String)map.get("page"))-1)*limit;
        map.put("page",page);
        map.put("limit",limit);
        List<Words> words = wordsMapper.getSelfWords(map);
        String code = (String) map.get("code");
        long count = wordsMapper.getSelfWordsCount(code);
        return CommonResult.success(words,count);
    }

    @Override
    public CommonResult addWords(Words words) {
        Words word = wordsMapper.getUserWord(words);
        if (word==null) {
            int result = wordsMapper.addWords(words);
            if (result > 0)
                return CommonResult.success(result, "添加成功");
            return CommonResult.failed("添加失败");
        }else
            return CommonResult.failed("您已提交过该单词");
    }

    @Override
    public CommonResult editWords(Words words) {
        int result = wordsMapper.editWords(words);
        if (result > 0)
            return CommonResult.success(result, "修改成功");
        return CommonResult.failed("修改失败");
    }

    @Override
    public CommonResult deleteWords(Integer id) {
        int result = wordsMapper.deleteWords(id);
        if (result > 0)
            return CommonResult.success(result, "删除成功");
        return CommonResult.failed("删除失败");
    }

    @Override
    public CommonResult getUserLikeWords(Map map) {
        int limit = Integer.parseInt((String)map.get("limit"));
        int page = (Integer.parseInt((String)map.get("page"))-1)*limit;
        map.put("page",page);
        map.put("limit",limit);
        List<Words> userLikeWords = wordsMapper.getUserLikeWords(map);
        int userId = Integer.parseInt((String) map.get("userId"));
        long count = wordsMapper.getUserLikeWordsCount(userId);
        return CommonResult.success(userLikeWords,count);
    }

    @Override
    public CommonResult getUserCollectWords(Map map) {
        int limit = Integer.parseInt((String)map.get("limit"));
        int page = (Integer.parseInt((String)map.get("page"))-1)*limit;
        map.put("page",page);
        map.put("limit",limit);
        List<Words> userCollectWords = wordsMapper.getUserCollectWords(map);
        int userId = Integer.parseInt((String) map.get("userId"));
        long count = wordsMapper.getUserCollectWordsCount(userId);
        return CommonResult.success(userCollectWords,count);
    }

    @Override
    public CommonResult getUserWord(Words words) {
        Words userWord = wordsMapper.getUserWord(words);
        return CommonResult.success(userWord);
    }
}
